package com.j.lemon.learn.asiainfo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @Author lijunjun
 * @Date 2020-06-11 14:58
 * @Description
 */
public class UdpReceive {
    public static void main(String[] args) throws IOException {
        //创建接收端Socket对象
        DatagramSocket ds = new DatagramSocket(6666);
        //接收数据
        byte[] bytes = new byte[1024];
        int length = bytes.length;
        DatagramPacket dp = new DatagramPacket(bytes, length);
        while (true){
            ds.receive(dp);
            //解析数据
            InetAddress address = dp.getAddress();
            byte[] data = dp.getData();
            int len = dp.getLength();
            //输出数据
            System.out.println(address);
            System.out.println(new String(data, 0, len));
        }

//        JSONArray jsonArray1 = new JSONArray();
//        jsonArray1.add("qweqweqwe");
//        jsonArray1.add("qweqweqwe1");
//        JSONArray jsonArray2 = new JSONArray();
//        jsonArray2.add("asdasdasd");
//        jsonArray2.add("asdasdasd1");
//        JSONObject jsonObject1 = new JSONObject();
//        jsonObject1.put("ugate",jsonArray1);
//        JSONObject jsonObject2 = new JSONObject();
//        jsonObject2.put("umap",jsonArray2);
//
//        JSONArray f = new JSONArray();
//        f.add(jsonObject1);
//        f.add(jsonObject2);
//
//        JSONObject j = new JSONObject();
//        j.put("regex",f);
//        System.out.println(j.toJSONString());
////        FileInputStream fileInputStream = new FileInputStream("/Users/lijunjun/Desktop/aaa.txt");
//        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//        String s = bufferedReader.readLine();
//        System.out.println("原始");
//        System.out.println(s);
//        String s1 = StringEscapeUtils.unescapeJava(s);
//        System.out.println("去转义");
//        System.out.println(s1);
//        JSONObject jsonObject = JSONObject.parseObject(s1);
//        System.out.println(jsonObject.toJSONString());

    }
}
